package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobaibai.common.UserCommon;
import com.xiaobaibai.entity.Product;
import com.xiaobaibai.entity.ProductEvaluation;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IProductEvaluationService;
import com.xiaobaibai.service.IProductService;
import com.xiaobaibai.threadPool.MyThreadPoolUnit;
import com.xiaobaibai.units.SecurityUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Api(tags = "商品接口")
@RequestMapping("/product")
@CrossOrigin
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductEvaluationService productEvaluationService;

    @Autowired
    private MyThreadPoolUnit threadPoolUnit;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/detail/{id}")
    @ApiOperation("单个详细商品")
    public ResponseFormat getProductDetail(@PathVariable(value = "id")Integer id){

        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        //存入浏览记录
        Integer userId= (Integer) authentication.getPrincipal();
        threadPoolUnit.setFavoritesRedis(userId.toString(),id.toString());

        //先获取商品
        Product product = productService.getProductById(id);
        //再获取两条评价
        List<ProductEvaluation> list = productEvaluationService.getTwoProductEvaluation(id);

        Map<String,Object> map=new HashMap<>();
        map.put("product",product);
        map.put("evaluation",list);

        return ResponseFormat.ok("获取商品数据成功",map);
    }

    @GetMapping("/evaluation/{id}")
    @ApiOperation("单个商品评价")
    public ResponseFormat getEvaluationByProductId(@PathVariable(value = "id")Integer id){

        List<ProductEvaluation> list = productEvaluationService.getListByProductId(id);

        return ResponseFormat.ok("获取商品评价成功",list);
    }

    @GetMapping("/product/by-category/{id}")
    @ApiOperation("根据分类查询商品")
    public ResponseFormat getProductByCategory(@PathVariable(value = "id")Integer id){
        return ResponseFormat.ok("根据分类获取商品成功",productService.getProductByCategory(id));
    }

    @GetMapping("/favorites")
    @ApiOperation("获取浏览记录")
    public ResponseFormat favoritesList(){
        Integer userId = SecurityUnit.getUserId();
        Set<String> set = stringRedisTemplate.opsForSet().members(UserCommon.favorites+userId);
        List<Product> products = productService.listByIds(set);
        return ResponseFormat.ok("足迹获取成功",products);
    }

    @PostMapping("/pick/{id}")
    @ApiOperation("收藏一个商品")
    public ResponseFormat pickProduct(@PathVariable(value = "id")Integer productId){
        Integer userId = SecurityUnit.getUserId();
        stringRedisTemplate.opsForList().leftPush(UserCommon.pick+userId,productId.toString());
        return ResponseFormat.ok("添加收藏成功");
    }

    @GetMapping("/pick/list")
    @ApiOperation("获取收藏的商品")
    public ResponseFormat pickList(){
        Integer userId = SecurityUnit.getUserId();
        List<String> list = stringRedisTemplate.opsForList().range(UserCommon.pick + userId, 0, -1);
        if(list==null||list.size()==0)
            return ResponseFormat.ok("收藏获取成功",new ArrayList<>());
        QueryWrapper<Product> q1=new QueryWrapper<>();
        q1.select("product_image","product_name","product_price","id","product_unit");
        q1.in("id",list);
        List<Product> products = productService.list(q1);
        return ResponseFormat.ok("收藏获取成功",products);
    }

    @DeleteMapping("/pick/{id}")
    @ApiOperation("取消收藏的商品")
    public ResponseFormat deletePrick(@PathVariable(value = "id")Integer productId){
        Integer userId=SecurityUnit.getUserId();
        //1从左往右删除第一个  2从右往左删除第一个  0删除全部
        stringRedisTemplate.opsForList().remove(UserCommon.pick + userId, 1, productId.toString());
        return ResponseFormat.ok("删除收藏成功");
    }


}
